package com.china08.yxyapi.api.manage.edu.platform.statistics.resourcepandect1;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceSubNameCountService;

/**
 * 
 * @Description 教育局平台办公 资源统计（饼状图）资源学科分布
 *
 * @Author LiuWeidong
 *
 * @Date 2017年12月8日
 *
 */
@CrossOrigin
@RestController
public class ResourceSubName1CountController extends YxyApiBaseController {

    @Autowired
    public ResourceSubNameCountService resourceSubNameCountService;
    @Autowired
    public YxyResSubjectRepository yxyResSubjectRepository;

    @RequestMapping(value = "/api_yw/manage/edu/platform/statistics/resourcepandect1/resourcesubname/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req) {

        RespModel model = new RespModel();
        Integer schoolBureauLevel = getProfile(request, response).getSchoolBureauLevel();
        String districtCode = null;
        if (schoolBureauLevel == 0) {
            districtCode = getProfile(request, response).getSchoolAreaIdDistrict();
        } else if (schoolBureauLevel == 1) {
            districtCode = getProfile(request, response).getSchoolAreaIdCity();
        } else if (schoolBureauLevel == 2) {
            districtCode = getProfile(request, response).getSchoolAreaIdProvince();
        }
        Map<String, Long> countLst = null;

        DecimalFormat df = new DecimalFormat("######0.00");

        // 饼形图
        List<Object[]> countLstObj = new ArrayList<Object[]>();

        countLst = resourceSubNameCountService.resourcePhaseCount(req.stageName, districtCode, schoolBureauLevel);
        Integer zong = 0;
        for (String key1 : countLst.keySet()) {
            zong += Integer.parseInt(countLst.get(key1).toString());
        }
        Object[] objs = null;
        for (String key : countLst.keySet()) {
            objs = new Object[2];
            YxyResSubject yxyResSubject = yxyResSubjectRepository.findById(key);

            if (yxyResSubject != null) {
                objs[0] = yxyResSubject.getName();
                objs[1] = Double.parseDouble(df.format(Double.parseDouble(countLst.get(key).toString())));
                countLstObj.add(objs);
            }

        }

        String[] subjectNameList = new String[] { "语文", "数学", "英语", "思想政治", "物理", "化学", "历史", "生物", "地理" };
        List<Object[]> subList = new ArrayList<Object[]>();
        if (countLstObj.size() > 0) {
            Object[] obj = null;
            for (String subjectName : subjectNameList) {

                for (Object[] objects : countLstObj) {
                    if (subjectName.equals(objects[0])) {
                        obj = new Object[2];
                        obj[0] = objects[0];
                        obj[1] = objects[1];
                        subList.add(obj);
                    }
                }
            }

        }
        // 将新组合的list里的学段名称值连接在一起
        String phaseName = "";
        for (Object[] objects : subList) {
            phaseName += objects[0] + ",";
        }
        if (countLstObj.size() > subList.size()) {
            for (Object[] countLstObj1 : countLstObj) {

                // 搜索字符串中是否包含数组里的元素
                if (phaseName.indexOf(countLstObj1[0].toString()) == -1) {
                    subList.add(countLstObj1);
                }
            }
        }
        model.countLstObj = subList;
        return model;

    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 学段名称
        public String stageName;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 资源学科饼形图的数据
        public List<Object[]> countLstObj;
    }
}
